package cn.tedu.oa.attendance.service.impl;

import cn.tedu.oa.attendance.mapper.AttendanceMapper;
import cn.tedu.oa.attendance.pojo.dto.query.AttendanceQuery;
import cn.tedu.oa.attendance.pojo.entity.Attendance;
import cn.tedu.oa.attendance.pojo.po.AttendancePO;
import cn.tedu.oa.attendance.service.AttendanceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
@Slf4j
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;
    @Override
    public void save(AttendancePO attendancePO) {
        log.debug("新增考勤业务");

        //特殊转换id
        if ("manager9881".equals(attendancePO.getUserId())){
            attendancePO.setUserId("106");
        }

        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 设置时间为 00:00:00
        LocalTime startOfDay = LocalTime.of(0, 0, 0);
        // 组合成 LocalDateTime
        LocalDateTime start = LocalDateTime.of(today, startOfDay);
        attendancePO.setWorkDate(start);

        //检测上下班记录》1为上班，2为下班
        Long attNum = checkType(attendancePO);
        if (attNum == 0) {
            attendancePO.setType(1);
            log.debug("新增上班记录:{}",attNum);
        }else {
            attendancePO.setType(2);
            log.debug("新增下班记录:{}",attNum);
        }

        int row = attendanceMapper.insert(attendancePO);
        log.debug("新增考勤成功,新增:{}",row);
    }

    //检测上下班记录
    private Long checkType(AttendancePO attendancePO) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",attendancePO.getUserId());
        wrapper.eq("work_date",attendancePO.getWorkDate());
        return attendanceMapper.selectCount(wrapper);
    }

    @Override
    public List<Attendance> getlist(AttendanceQuery query) {
        QueryWrapper wrapper = new QueryWrapper<>();
        if(query != null){
            log.info("查询考勤:{}", query);
            if(query.getId() != null){
                wrapper.eq("id",query.getId());
            }
            if(query.getUserId() != null){
                wrapper.eq("user_id",query.getUserId());
            }
            if(query.getStartTime() != null){
                wrapper.ge("check_time",query.getStartTime());
            }
            if(query.getEndTime() != null){
                wrapper.le("check_time",query.getEndTime());
            }
            if(query.getWorkDate() != null){
                wrapper.eq("work_date",query.getWorkDate());
            }
            if (query.getType() != null){
                wrapper.eq("type",query.getType());
            }
            wrapper.orderByDesc("check_time");
        }
        List<Attendance> list = attendanceMapper.selectList(wrapper);
        return list;
    }
}
